const Actions = require('./actions');
const DB = require('app/db');
import { monStorage, Storage } from 'clientConfig/util/StoreData';
import { guid, getTime, getMarkdownText, sendNotice, dealTime } from 'clientConfig/util/queryurlfield';
import { Control } from 'react-keeper';
import { NEW, RUNNING } from 'clientConfig/constants'
const { Toast } = SaltUI;
module.exports = Reflux.createStore({
    listenables: [Actions],
    data: {

    },
    onSubmit: function (params, cb) {
        let t = this;
        let required_params = this.fit(params);
        //   debugger
        let notice_info = {
            "process_id": required_params.process_id,
            "proj_num": required_params.proj_num,
            "proj_name": required_params.proj_name,
            "create_time": required_params.create_time,
            "status": required_params.status,
            "type": params.pagedata ? "变更申请" : "待我审批的"
        }
        // debugger
        if (params.pagedata) {
            required_params = this.modify(required_params, params);
        }
        let noticed_lists = required_params.approvers_id[required_params.next_cursor];
        DB.Approval.send_approval(required_params)
            .then((content) => {
                if (!content) {
                    Toast.show({
                        type: 'success',
                        content: params.edit ? '你的变更申请已提交' : '你的合同签订申请已提交'
                    });
                    sendNotice(noticed_lists, notice_info)
                    Storage.set(params.data.proName, required_params.proj_webhook);
                    Control.go(-1)
                }
            })
            .catch((error) => {
                if (error.errcode == 2) {
                    Toast.show({
                        type: 'error',
                        content: "合同签订项目正在审核中"
                    });
                }
            });
    },
    fit: function (params) {
        let agent_id = Storage.get('user.agentid');
        let create_time = getTime();
        let finish_time = "";
        let originator_user = monStorage.get('user.nickname');
        let originator_user_id = Storage.get('user.id') //审批发起人的userid  
        // debugger
        let dept_id = monStorage.get('user.department');
        let approvers = params.data.approvers;
        let approvers_id = params.data.approvers_id;
        let cc_list = "";
        let cc_list_id = [];
        var array = [];
        for (let i = 0; i < approvers.length; i++) {
            array.push(JSON.stringify({ content: "", file: "", imgae: "" }));
        }
        let process_result = array.length > 1 ? array : JSON.stringify({ content: "", file: "", imgae: "" });
        let next_cursor = 0;//整型
        const status = NEW;//整型
        const module_type = "workplan";
        const proj_category = "software";
        const proj_num = params.data.proNum;
        const proj_name = params.data.proName;
        var proj_webhook = params.data.robot
        const process_id = guid();
        const proj_markdown = getMarkdownText({ create_time, originator_user, approvers: approvers[0], proj_name, proj_num, status, title: "计划管理" })
        var nodelists = []//必传数组
        for (let i = 0; i < params.node.length; i++) {
            var node = {};
            node.node_name = params.node[i].name;
            node.node_state = 1;//整型
            node.modified = "0";
            node.permission = "1";
            node.node_achievements = [];
            let node_info = [];
            let node_array = Object.keys(params.node[i]);
            for (let j = 0; j < node_array.length; j++) {
                if (j == 0) continue;
                let info = {};
                info.name = node_array[j] == "startTime" ? "开始时间" : "结束时间";
                info.value = dealTime(params.data[params.node[i][node_array[j]]]);
                node_info.push(info);
            }
            node.node_info = node_info;
            nodelists.push(node);
        }
        // debugger
        let proj_create_time = dealTime(params.data.proStartTime)//new Date(params.data.proTime).toLocaleDateString();
        let attach_info = JSON.stringify(params.upfileName);
        // debugger
        let form_component_values = {
            content: { proj_create_time, attach_info },
            nodelists: nodelists
        }

        return Object.assign({}, { agent_id, originator_user_id, create_time, finish_time, originator_user, dept_id, approvers, approvers_id, cc_list, cc_list_id, process_result, next_cursor, status, module_type, proj_category, proj_num, proj_name, proj_webhook, process_id, form_component_values, proj_markdown })
    },
    modify: function (new_data, old_data) {
        let edit = old_data.edit,
        title = '计划管理';
        old_data = old_data.pagedata;
        new_data.proj_category = "changereq-software";
        if (!new_data.approvers_id && !!old_data.approvers_id) new_data.approvers_id = old_data.approvers_id;
        let old_change = [];//变更前字段
        let new_change = [];//变更后字段
        let form_component_values_new = new_data.form_component_values;
        let form_component_values_old = old_data.form_component_values;
        let nodelists_new = form_component_values_new.nodelists;
        let nodelists_old = form_component_values_old.nodelists;
        for (let i = 0; i < nodelists_new.length; i++) {
            let node_info_new = nodelists_new[i].node_info;
            let node_info_old = nodelists_old[i].node_info;
            for (let j = 0; j < node_info_new.length; j++) {
                if (node_info_new[j].value != node_info_old[j].value) {
                    node_info_new[j].ext_value = node_info_old[j].value//变更过
                    old_change.push(node_info_old[j].value);
                    new_change.push(node_info_new[j].value);
                }
            }
        }
        form_component_values_new.nodelists = nodelists_new;
        form_component_values_new.content.changerecord_type = JSON.stringify({
            type: edit ? "撤销申请" : "内容变更",
            old_process_id: old_data.process_id,
            old_proj_category: old_data.proj_category
        })
        new_data.form_component_values = form_component_values_new;
        let { create_time, originator_user, approvers, approvers_id, next_cursor, proj_name, proj_num, status } = { ...new_data };
        const proj_markdown = getMarkdownText({ title, old_change, new_change, change_type: edit ? "撤销申请" : "内容变更", create_time, originator_user, approvers: approvers[next_cursor], proj_name, proj_num, status }, "change")
        new_data.proj_markdown = proj_markdown;
        return new_data;
    },
    updateComponent: function () {
        this.trigger(this.data);
    }
});
